package com.site.wisdom_construction.controller.personnelModule;

import com.site.wisdom_construction.entity.personnelModule.SafetyTraining;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.responseEntity.SafetyTrainingResult;
import com.site.wisdom_construction.service.personnelModule.SafetyTrainingService;
import com.site.wisdom_construction.service.personnelModule.SafetyTrainingWorkersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/** @Author daiyifei @Date 2022/1/5 @Description */
@Api(tags = "安全教育培训接口")
@RestController
@RequestMapping("SafetyTraining")
@CrossOrigin
public class SafetyTrainingController {
  @Autowired private SafetyTrainingService safetyTrainingService;

  @Autowired private SafetyTrainingWorkersService safetyTrainingWorkersService;

  @ApiOperation(value = "增加安全培训教育记录", notes = "测试数据:")
  @PostMapping("/insertSafetyTraining")
  public JsonResult insertSafetyTraining(@RequestBody SafetyTraining safetyTraining) {
    System.out.println("safetyTraining:"+safetyTraining);
    JsonResult jsonResult = new JsonResult();
    // 获取人员数组
    List list = safetyTraining.getList();

    // 增加安全教育培训记录
    int i = safetyTrainingService.insertSafetyTraining(safetyTraining);
    // 返回新增加的安全教育培训记录id
    int id = safetyTraining.getId();
    if (i == 1) {
      Map mapR = new HashMap();
      mapR.put("safetyTrainingId", id);
      for (int l = 0; l < list.size(); l++) {
        mapR.put("contractWorkersId", list.get(l));
        // 添加人员培训对应信息
        safetyTrainingWorkersService.insertSafetyTrainingWorkers(mapR);
      }
      jsonResult.setCode(200);
      jsonResult.setMessage("成功录入安全培训教育记录!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("录入安全培训教育记录失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "删除安全培训教育记录", notes = "测试数据:")
  @PostMapping("/deleteSafetyTraining")
  public JsonResult deleteSafetyTraining(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    List<Integer> list = (List<Integer>) map.get("list");
    int l = 0;
    Map mapR = new HashMap();
    for (int i = 0;i<list.size();i++){
      mapR.put("id",list.get(i));
      int a = safetyTrainingService.deleteSafetyTraining(mapR);
      l+=a;
    }
    map.put("safetyTrainingId", map.get("id"));
    if (l>=1) {
      safetyTrainingWorkersService.deleteSafetyTrainingWorkersById(map);
      jsonResult.setCode(200);
      jsonResult.setMessage("成功删除安全培训教育记录!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("删除安全培训教育记录失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "修改安全培训教育记录", notes = "测试数据:")
  @PostMapping("/updateSafetyTraining")
  public JsonResult updateSafetyTraining(@RequestBody SafetyTraining safetyTraining) {
    System.out.println("safetyTraining:"+safetyTraining);
    Map map = new HashMap();
    JsonResult jsonResult = new JsonResult();
    //获取前端发送的数组
    List<Integer> integerList = safetyTraining.getList();
    //数组去重
    List<Integer> list = integerList.stream().distinct().collect(Collectors.toList());
    //修改培训记录信息
    int i = safetyTrainingService.updateSafetyTraining(safetyTraining);
    if (i == 1) {
      map.put("safetyTrainingId", safetyTraining.getId());
      //删除之前的培训对应的人员
      int l = safetyTrainingWorkersService.deleteSafetyTrainingWorkersById(map);
      if(l>0){
        Map mapR = new HashMap();
        mapR.put("safetyTrainingId", safetyTraining.getId());
        for (int q = 0; q < list.size(); q++) {
          mapR.put("contractWorkersId", list.get(q));
          // 添加人员培训对应信息
          safetyTrainingWorkersService.insertSafetyTrainingWorkers(mapR);
        }
        jsonResult.setCode(200);
        jsonResult.setMessage("成功修改安全培训教育记录!");
      }else {
        jsonResult.setCode(20006);
        jsonResult.setMessage("培训人员修改失败!");
      }

    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("修改安全培训教育记录失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "分页模糊查询安全教育培训记录", notes = "测试数据:")
  @PostMapping("/findSafetyTrainingByPage")
  public Page findSafetyTrainingByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(safetyTrainingService.Total(map));
    page.setItems(safetyTrainingService.findSafetyTrainingByPage(map));
    return page;
  }

  @ApiOperation(value = "根据id查询安全教育培训记录(修改时)", notes = "测试数据:")
  @PostMapping("/findSafetyTrainingById")
  public OneDataResult findSafetyTrainingById(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    map.put("safetyTrainingId", map.get("id"));
    SafetyTrainingResult safetyTrainingResult = safetyTrainingService.findSafetyTrainingById(map);
    safetyTrainingResult.setSafetyTrainingWorkersList(
        safetyTrainingWorkersService.findSafetyTrainingWorkersById(map));
    oneDataResult.setData(safetyTrainingResult);
    return oneDataResult;
  }
}
